package com.have.blog.admin.controller;

import com.have.blog.admin.annotation.operationLogger.OperationLogger;
import com.have.blog.admin.domain.dto.ResourceCategoryDto;
import com.have.blog.admin.service.ResourceCategoryService;
import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.Delete;
import com.have.blog.base.validator.group.GetList;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.validator.group.Update;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.core.annotion.avoidRepeatableCommit.AvoidRepeatableCommit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value = "资源分类相关接口", tags = {"资源分类相关接口"})
@RestController
@RequestMapping("/resourceSort")
@Slf4j
public class ResourceCategoryController {
    @Autowired
    private ResourceCategoryService resourceCategoryService;

    @ApiOperation(value = "获取资源分类列表", notes = "获取资源分类列表", response = String.class)
    @PostMapping("/getList")
    public Wrapper<CommonPage> getList(@Validated({GetList.class}) @RequestBody ResourceCategoryDto resourceCategoryDto, BindingResult result) {

        ThrowableUtils.checkParamArgument(result);
        log.info("获取资源分类列表:{}", resourceCategoryDto);
        List<ResourceCategoryDto> list = resourceCategoryService.getList(resourceCategoryDto);
        return Wrapper.success(CommonPage.restPage(list));
    }

    @AvoidRepeatableCommit
    @OperationLogger(value = "增加资源分类")
    @ApiOperation(value = "增加资源分类", notes = "增加资源分类", response = String.class)
    @PostMapping("/add")
    public Wrapper add(@Validated({Insert.class}) @RequestBody ResourceCategoryDto resourceCategoryDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("增加资源分类:{}", resourceCategoryDto);
        return resourceCategoryService.addResourceCategory(resourceCategoryDto);
    }

    @OperationLogger(value = "编辑资源分类")
    @ApiOperation(value = "编辑资源分类", notes = "编辑资源分类", response = String.class)
    @PostMapping("/edit")
    public Wrapper edit(@Validated({Update.class}) @RequestBody ResourceCategoryDto resourceCategoryDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("编辑资源分类:{}", resourceCategoryDto);
        return resourceCategoryService.editResourceCategory(resourceCategoryDto);
    }

    @OperationLogger(value = "批量删除资源分类")
    @ApiOperation(value = "批量删除资源分类", notes = "批量删除资源分类", response = String.class)
    @PostMapping("/deleteBatch")
    public Wrapper delete(@Validated({Delete.class}) @RequestBody List<ResourceCategoryDto> resourceCategoryDtoList, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("批量删除资源分类:{}", resourceCategoryDtoList);
        return resourceCategoryService.deleteBatchResourceCategory(resourceCategoryDtoList);
    }

    @OperationLogger(value = "置顶资源分类")
    @ApiOperation(value = "置顶分类", notes = "置顶分类", response = String.class)
    @PostMapping("/stick")
    public Wrapper stick(@Validated({Delete.class}) @RequestBody ResourceCategoryDto resourceCategoryDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("置顶分类:{}", resourceCategoryDto);
        return resourceCategoryService.stickResourceCategory(resourceCategoryDto);
    }
}
